//package com.sangeng.config;
//
//import com.sangeng.filter.JwtAuthenticationTokenFilter;
//import com.sangeng.handler.security.AccessDeniedHandlerImpl;
//import com.sangeng.handler.security.AuthenticationEntryPointImpl;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.http.HttpMethod;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
//import org.springframework.security.config.http.SessionCreationPolicy;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.security.crypto.password.PasswordEncoder;
//import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
//
//@Configuration
//public class SecurityConfig extends WebSecurityConfigurerAdapter {
//
//    @Autowired
//    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
//
//    @Autowired
//    private AuthenticationEntryPointImpl authenticationEntryPoint;
//
//    @Autowired
//    private AccessDeniedHandlerImpl accessDeniedHandler;
//
//    @Override
//    protected void configure(HttpSecurity http) throws Exception {
//        http
//                //关闭csrf
//                .csrf().disable()
//                //不通过Session获取SecurityContext
//                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
//                .and()
//                .authorizeRequests()
//                // 对于登录接口 允许匿名访问
//                .antMatchers("/login").anonymous()
//                // 不允许匿名访问
//                .antMatchers("/logout").authenticated()
//                .antMatchers(HttpMethod.POST, "/comment").authenticated()
//                .antMatchers(HttpMethod.GET, "/user/userInfo").authenticated()
//                .antMatchers(HttpMethod.POST, "/upload").authenticated()
//                // 除上面外的所有请求全部不需要认证即可访问
//                .anyRequest().permitAll();
//
//        // 添加登录token过滤器
//        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
//        // 添加认证失败和授权失败的处理器
//        http.exceptionHandling()
//                .accessDeniedHandler(accessDeniedHandler)
//                .authenticationEntryPoint(authenticationEntryPoint);
//        // 关闭httpLogout
//        http.logout().disable();
//        //允许跨域
//        http.cors();
//    }
//
//    // 添加 加密器
//    @Bean
//    public PasswordEncoder passwordEncoder() {
//        return new BCryptPasswordEncoder();
//    }
//
//    // 添加 AuthenticationManager
//    @Override
//    @Bean
//    public AuthenticationManager authenticationManagerBean() throws Exception {
//        return super.authenticationManagerBean();
//    }
//
//
//}
